
package com.sun.alen.service.Impl;

import com.sun.alen.entity.*;
import com.sun.alen.mapper.CourseMapper;
import com.sun.alen.mapper.ExamMapper;
import com.sun.alen.mapper.MainClassMapper;
import com.sun.alen.mapper.StudentMapper;
import com.sun.alen.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private MainClassMapper mainClassMapper;
    @Override
    public boolean add(Exam exam) {
        return examMapper.add(exam);
    }

    @Override
    public boolean update(Exam exam) {
        return examMapper.update(exam);
    }

    @Override
    public boolean delete(String id) {
        return examMapper.delete(id);
    }

    @Override
    public PageBean search(String input, String select, Integer currentPage, Integer pageSize) {
        int start = (currentPage-1)*pageSize;
        List<Exam> examList = new ArrayList<>();
        int endIndex=0;
        long count = 0;
        input = "%" + input + "%";
        if (input.equals("%%")) {
            examList = examMapper.list(start, pageSize);
            count = examMapper.count();
        } else {
            switch (select) {
                case "1" -> {
                    List<Student> studentList = studentMapper.searchIdByStudent(input);
                    for (Student student : studentList) {
                        List<Exam> tempList = examMapper.searchByStudent(student.getStudentId());
                        examList.addAll(tempList);
                    }
                    count = examList.size();
                    endIndex = Math.min(start + pageSize, examList.size());
                    examList = examList.subList(start,endIndex);
                }
                case "2" -> {
                    List<Course> courseList = courseMapper.searchIdByStudent(input);
                    for (Course course:courseList) {
                        List<Exam> tempList = examMapper.searchByCourse(course.getCourseId());
                        examList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, examList.size());
                    count = examList.size();
                    examList = examList.subList(start,endIndex);
                }
                default -> {
                    examList = examMapper.list((currentPage - 1) * pageSize, pageSize);
                    count = examMapper.count();
                }
            }
        }
        return new PageBean(count, examList);
    }


    @Override
    public PageBean searchTeacher(String input, String select, Integer teacherId, Integer currentPage, Integer pageSize) {
            Integer start = (currentPage - 1) * pageSize;
            int endIndex = 0;
            List<Exam> examList=new ArrayList<>();
            long count=0;
            input="%"+input+"%";
            switch (select){
                case "1"-> {
                    List<Course> courseList = courseMapper.searchAllByCourseName(input);
                    System.out.println(courseList);
                    for (int i = 0; i < courseList.size(); i++) {
                        List<Exam> tempList = examMapper.searchByCourseId(courseList.get(i).getCourseId());
                        examList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, examList.size());
                    count = examList.size();
                    examList = examList.subList(start, endIndex);
                }
                case "2"-> {
                    List<Student> studentList = studentMapper.searchByStudentName(input,teacherId);
                    for (int i = 0; i < studentList.size(); i++) {
                        List<Exam> tempList = examMapper.searchByStudentId(studentList.get(i).getStudentId());
                        examList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, examList.size());
                    count = examList.size();
                    examList = examList.subList(start,endIndex);
                }

                default-> {
                    List<Student> studentList1 =studentMapper.searchByTeacherId(teacherId);
                    for (int i = 0; i < studentList1.size(); i++) {
                        List<Exam> tempList = examMapper.searchByStudentId(studentList1.get(i).getStudentId());
                        examList.addAll(tempList);
                    }
                    endIndex = Math.min(start + pageSize, examList.size());
                    count = examList.size();
                    examList = examList.subList(start,endIndex);
                }

            }
            PageBean pageBean = new PageBean(count,examList);
            return pageBean;
        }
    }
